﻿

namespace FileMergerApp
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            throw new NotImplementedException();
        }

        private void btnSelectFile1_Click(object sender, EventArgs e)
        {
            // 打开文件选择对话框，选择第一个文件
            OpenFileDialog openFileDialog = new OpenFileDialog
            {
                Title = "选择文件 1"
            };
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtFilePath1.Text = openFileDialog.FileName; // 显示选中文件路径
            }
        }

        private void btnSelectFile2_Click(object sender, EventArgs e)
        {
            // 打开文件选择对话框，选择第二个文件
            OpenFileDialog openFileDialog = new OpenFileDialog
            {
                Title = "选择文件 2"
            };
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtFilePath2.Text = openFileDialog.FileName; // 显示选中文件路径
            }
        }

        private void btnMergeFiles_Click(object sender, EventArgs e)
        {
            string file1Path = txtFilePath1.Text;
            string file2Path = txtFilePath2.Text;

            // 检查是否选择了两个文件
            if (string.IsNullOrEmpty(file1Path) || string.IsNullOrEmpty(file2Path))
            {
                MessageBox.Show("请先选择两个文件！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            try
            {
                // 获取程序目录并创建 Data 子目录
                string appDirectory = AppDomain.CurrentDomain.BaseDirectory;
                string dataDirectory = Path.Combine(appDirectory, "Data");

                // 如果 Data 目录不存在，则创建它
                if (!Directory.Exists(dataDirectory))
                {
                    Directory.CreateDirectory(dataDirectory);
                }

                // 合并后的文件路径
                string mergedFilePath = Path.Combine(dataDirectory, "mergedFile.txt");

                // 读取文件内容并合并
                string file1Content = File.ReadAllText(file1Path);
                string file2Content = File.ReadAllText(file2Path);
                string mergedContent = file1Content + Environment.NewLine + file2Content;

                // 将合并后的内容写入新文件
                File.WriteAllText(mergedFilePath, mergedContent);

                // 显示合并后的文件路径
                MessageBox.Show("文件已成功合并！合并文件路径: " + mergedFilePath, "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("发生错误: " + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
